package com.mvp.lt.airlineview.wayline;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.view.LayoutInflater;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.Circle;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.luck.picture.lib.tools.DateUtils;
import com.mvp.lt.airlineview.App;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.databinding.DroneMiddleMarkerBinding;
import com.mvp.lt.airlineview.databinding.DroneRegionPointMarkerBinding;
import com.mvp.lt.airlineview.location.MapMarkerTypeConstant;
import com.mvp.lt.airlineview.map.MapCalculationLogLatDistance;
import com.mvp.lt.airlineview.map.MapCreateManager;
import com.mvp.lt.airlineview.map.ObliquePhotographyUtil;
import com.mvp.lt.airlineview.utils.AndroidUtil;
import com.mvp.lt.airlineview.utils.LogUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by MapShadowUtils.
 * User: Administrator
 * Name: AirLineView
 * functiona:带状航线
 * Date: 2019/10/16 0016
 * Time: 下午 17:24
 */
public class BandWayLineUtil {
    private static String TAG = "带状航线";
    private AMap m3DAMap;
    /**
     * 航线间隔 m
     */
    private int routeIntervalDistance = 20;
    /**
     * 航向间距 m
     */
    private int courseIntervalDistance = 20;
    /**
     * 中心线
     */
    private List<Polyline> mCenterPolyline = new ArrayList<>();
    //左外扩 米
    private double leftExpansion = 50;
    //右外扩 米
    private double rightExpansion = 50;

    /**
     * GSD
     */
    private double baseGsd = 2.74;
    private double gsd = 2.74;
    /**
     * 盘向重叠率 80%  [10,90]
     */
    private int diskOverlapRatio = 80;
    /**
     * 航向重叠率 计算拍照间隔[10,90]
     */
    private int courseOverlapRatio = 80;
    /**
     * 飞行高度 m
     */
    private int flyHeight = 50;

    /**
     * 等效焦距 单位毫米
     */
    private double focalLength = 24;
    /**
     * 长画幅 单位毫米
     */
    private int longFormat = 35;
    /**
     * 短画幅 单位毫米
     */
    private int shortFormat = 24;
    /**
     * 速度
     */
    private int flySpeed = 6;
    /**
     * 起始点
     */
    private Marker mStartMarker;
    /**
     * 结束点
     */
    private Marker mEndMarker;
    /**
     * 中心点集合
     */
    private List<Marker> mMarkerList = new ArrayList<>();
    /**
     * 中心点
     */
    private List<Marker> markerMiddleWayPoints = new ArrayList<>();
    private List<Marker> markerMiddles = new ArrayList<>();
    /**
     * 中心点集合
     */
    private List<LatLng> mCenterLatLngs = new ArrayList<>();

    /**
     * 加号点集合
     */
    private List<LatLng> mMiddleWayPointsLatLngs = new ArrayList<>();
    /**
     * 航线
     */
    private List<LatLng> wayPointLineList = new ArrayList<>();

    /**
     * 中心线拐角 角度
     */
    private List<Double> mAngles = new ArrayList<>();
    /**
     * 左边缘线集合
     */
    private List<LatLng> mLeftLatLngs = new ArrayList<>();
    /**
     * 右边缘线集合
     */
    private List<LatLng> mRightLatLngs = new ArrayList<>();
    /**
     * 初始化 右航线集合
     */
    private List<LatLngLineBean> rightWayLinesBeans = new ArrayList<>();
    /**
     * 初始化
     */
    private List<Polyline> rightWayLinesPolyline = new ArrayList<>();
    /**
     * 初始化 左航线集合
     */
    private List<LatLngLineBean> leftWayLinesBeans = new ArrayList<>();
    /**
     * 初始化
     */
    private List<Polyline> leftWayLinesPolyline = new ArrayList<>();
    private List<Marker> leftWayLinesMarkers = new ArrayList<>();
    /**
     * 航线
     */
    private Polyline mLeftPolyline;
    /**
     * 航线
     */
    private Polyline mRightPolyline;
    /**
     * 封闭线
     */
    private Polyline mStartPolyline;
    /**
     * 封闭线
     */
    private Polyline mEndPolyline;
    /**
     * 航线
     */
    private Polyline mWayLinePolyline;
    /**
     * 索引
     */
    private int revokePosition = -1;
    private LatLng mStartLatLng;

    public BandWayLineUtil(AMap m3DAMap) {
        this.m3DAMap = m3DAMap;
        leftWayLinesBeans = new ArrayList<>();
        rightWayLinesBeans = new ArrayList<>();
        setRouteCourseIntervalDistance(flyHeight, focalLength, shortFormat, courseOverlapRatio);
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
        setGsd(flyHeight);
    }

    /**
     * 地图打点
     *
     * @param latLng
     */
    public void addLatLngAddDrawWayLine(LatLng latLng) {
        revokePosition = mCenterLatLngs.size();
        drawCenterPointLine(latLng);
        calculationWayLine();
        drawLeftEdgStripLineToMap();
        drawRightEdgStripLineToMap();
        drawEdgClosedLineToMap();
        drawFlyWayLine();      //画全部航线
    }

    public void clearMap() {
        if (mWayLinePolyline != null) {
            mWayLinePolyline.remove();
            mWayLinePolyline = null;
        }
        if (mStartPolyline != null) {
            mStartPolyline.remove();
            mStartPolyline = null;
        }
        if (mEndPolyline != null) {
            mEndPolyline.remove();
            mEndPolyline = null;
        }
        if (mCenterLatLngs != null) {
            mCenterLatLngs.clear();
        }
        if (mRightPolyline != null) {
            mRightPolyline.remove();
            mRightPolyline = null;
        }
        if (mLeftPolyline != null) {
            mLeftPolyline.remove();
            mLeftPolyline = null;
        }
        if (mCenterPolyline != null) {
            for (int i = 0; i < mCenterPolyline.size(); i++) {
                mCenterPolyline.get(i).remove();
            }
            mCenterPolyline.clear();
        }
        if (mStartMarker != null) {
            mStartMarker.remove();
            mStartMarker = null;
        }

        if (mEndMarker != null) {
            mEndMarker.remove();
            mEndMarker = null;
        }
        wayPointLineList.clear();
        for (int i = 0; i < mMarkerList.size(); i++) {
            mMarkerList.get(i).destroy();
        }
        mMarkerList.clear();

        for (int i = 0; i < markerMiddleWayPoints.size(); i++) {
            markerMiddleWayPoints.get(i).destroy();
        }
        markerMiddleWayPoints.clear();
        mMiddleWayPointsLatLngs.clear();
        if (mLeftLatLngs != null) {
            mLeftLatLngs.clear();
        }
        if (mRightLatLngs != null) {
            mRightLatLngs.clear();
        }
        if (mAngles != null) {
            mAngles.clear();
        }
        if (leftWayLinesBeans != null) {
            leftWayLinesBeans.clear();
        }
        if (rightWayLinesBeans != null) {
            rightWayLinesBeans.clear();
        }

        if (rightWayLinesPolyline != null) {
            for (int i = 0; i < rightWayLinesPolyline.size(); i++) {
                rightWayLinesPolyline.get(i).remove();
            }
            rightWayLinesPolyline.clear();
        }

        if (leftWayLinesPolyline != null) {
            for (int i = 0; i < leftWayLinesPolyline.size(); i++) {
                leftWayLinesPolyline.get(i).remove();
            }
            leftWayLinesPolyline.clear();
        }
        if (mStripLineChangeListener != null) {
            mStripLineChangeListener.OnStripLineChange();
        }
    }

    /**
     * 点击加号重画
     */
    public void reClickAddMarkerDrawPoint() {
        List<LatLng> tempCenterLatLng = new ArrayList<>(mCenterLatLngs);
        for (int i = 0; i < markerMiddleWayPoints.size(); i++) {
            markerMiddleWayPoints.get(i).destroy();
        }
        markerMiddleWayPoints.clear();
        mMiddleWayPointsLatLngs.clear();
        for (int i = 0; i < mMarkerList.size(); i++) {
            mMarkerList.get(i).destroy();
        }
        mMarkerList.clear();
        if (mCenterPolyline != null) {
            for (int i = 0; i < mCenterPolyline.size(); i++) {
                mCenterPolyline.get(i).remove();
            }
            mCenterPolyline.clear();
        }
        reDrawMap();
        if (tempCenterLatLng.size() < 2) {
            mWayLinePolyline.remove();
            mWayLinePolyline = null;
            mStartMarker.remove();
            mStartMarker = null;
            mEndMarker.remove();
            mEndMarker = null;
            mLeftPolyline.remove();
            mLeftPolyline = null;
            mRightPolyline.remove();
            mRightPolyline = null;
            mStartPolyline.remove();
            mStartPolyline = null;
            mEndPolyline.remove();
            mEndPolyline = null;
        }
        if (tempCenterLatLng.size() > 0) {
            for (int i = 0; i < tempCenterLatLng.size(); i++) {
                LatLng latLng = tempCenterLatLng.get(i);
                drawCenterPointLine(latLng);
                calculationWayLine();
            }
            drawLeftEdgStripLineToMap();
            drawRightEdgStripLineToMap();
            drawEdgClosedLineToMap();
            //画全部航线
            drawFlyWayLine();
        }
    }

    public void reDrawPoint() {
        List<LatLng> tempCenterLatLng = new ArrayList<>(mCenterLatLngs);
        reDrawMap();
        for (int i = 0; i < tempCenterLatLng.size(); i++) {
            LatLng latLng = tempCenterLatLng.get(i);
            mCenterLatLngs.add(latLng);
            calculationWayLine();
        }
        drawLeftEdgStripLineToMap();
        drawRightEdgStripLineToMap();
        drawEdgClosedLineToMap();
        //画全部航线
        drawFlyWayLine();
    }

    public void setRightExpansion(double rightExpansion) {
        this.rightExpansion = rightExpansion;
        List<LatLng> tempCenterLatLng = new ArrayList<>(mCenterLatLngs);
        reDrawMap();
        for (int i = 0; i < tempCenterLatLng.size(); i++) {
            LatLng latLng = tempCenterLatLng.get(i);
            mCenterLatLngs.add(latLng);
            calculationWayLine();
        }
        drawLeftEdgStripLineToMap();
        drawRightEdgStripLineToMap();
        drawEdgClosedLineToMap();
        //画全部航线
        drawFlyWayLine();
    }


    public void setLeftExpansion(double leftExpansion) {
        this.leftExpansion = leftExpansion;
        List<LatLng> tempCenterLatLng = new ArrayList<>(mCenterLatLngs);
        reDrawMap();
        for (int i = 0; i < tempCenterLatLng.size(); i++) {
            LatLng latLng = tempCenterLatLng.get(i);
            mCenterLatLngs.add(latLng);
            calculationWayLine();
        }
        drawLeftEdgStripLineToMap();
        drawRightEdgStripLineToMap();
        drawEdgClosedLineToMap();
        //画全部航线
        drawFlyWayLine();
    }


    public void reDrawMap() {
        if (mCenterLatLngs != null) {
            mCenterLatLngs.clear();
        }
        if (mLeftLatLngs != null) {
            mLeftLatLngs.clear();
        }
        if (mRightLatLngs != null) {
            mRightLatLngs.clear();
        }
        if (mAngles != null) {
            mAngles.clear();
        }
        if (leftWayLinesBeans != null) {
            leftWayLinesBeans.clear();
        }
        if (rightWayLinesBeans != null) {
            rightWayLinesBeans.clear();
        }
    }


    private void calculationWayLine() {
        wayPointLineList.clear();
        if (mCenterLatLngs.size() > 1) {// 0 1
            LogUtil.e("航点", "==============>" + mCenterLatLngs.size());
            LatLng latLng1 = mCenterLatLngs.get(mCenterLatLngs.size() - 2);
            LatLng latLng2 = mCenterLatLngs.get(mCenterLatLngs.size() - 1);
            //获取角度
            double angle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
            mAngles.add(angle);
            //左右边线 =======================================================/
            getLeftEdgFlightBeltLine(latLng1, latLng2, angle);
            getRightEdgFlightBeltLine(latLng1, latLng2, angle);
            //计算左右边缘线
            if (mCenterLatLngs.size() > 2) {
                LatLng latLng0 = mCenterLatLngs.get(mCenterLatLngs.size() - 3);
                int innerAngle = (int) MapCalculationLogLatDistance.getTriangleAngle(latLng0.latitude, latLng0.longitude, latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
                double angleDifference = mAngles.get(mAngles.size() - 2) - mAngles.get(mAngles.size() - 1);
                boolean isRightTurn = (angleDifference > 0 && Math.abs(angleDifference) <= 180) || (angleDifference < 0 && Math.abs(angleDifference) > 180);
                calculationLeftEdgPointPolyLine(innerAngle, isRightTurn);
                calculationRightEdgPointPolyLine(innerAngle, isRightTurn);
            }
            for (int i = 0; i < markerMiddles.size(); i++) {
                markerMiddles.get(i).remove();
            }
            markerMiddles.clear();
            for (int i = 0; i < mLeftLatLngs.size(); i++) {
                Marker marker = ObliquePhotographyUtil.getInstance().addMarker(App.getContext(), mLeftLatLngs.get(i), i + 1, false);
                markerMiddles.add(marker);
            }

            for (int i = 0; i < mRightLatLngs.size(); i++) {
                Marker marker = ObliquePhotographyUtil.getInstance().addMarker(App.getContext(), mRightLatLngs.get(i), i + 1, false);
                markerMiddles.add(marker);
            }
            //左 =========================**********************************===================================/
            getLeftLinePoint(latLng1, latLng2, angle, mCenterLatLngs.size());
            //计算左航线
            if (mCenterLatLngs.size() > 2) {
                LatLng latLng00 = mCenterLatLngs.get(mCenterLatLngs.size() - 3);
                calculationLeftWayPointPolyLine(latLng00, latLng1, latLng2);
            }
            calculationLeftWayPolyLine();
            //中线
            if (leftWayLinesBeans.size() % 2 == 0) {
                wayPointLineList.addAll(mCenterLatLngs);
            } else {
                List<LatLng> tempCenterLatLng = new ArrayList<>(mCenterLatLngs);
                Collections.reverse(tempCenterLatLng);
                wayPointLineList.addAll(tempCenterLatLng);
            }

            //右 ============================================================/
            getRightLinePoint(latLng1, latLng2, angle, mCenterLatLngs.size());
            //计算右航线
            if (mCenterLatLngs.size() > 2) {
                LatLng latLng00 = mCenterLatLngs.get(mCenterLatLngs.size() - 3);
                calculationRightWayPointPolyLine(latLng00, latLng1, latLng2);
            }
            calculationRightWayPolyLine();

            LogUtil.e("航点", "==============>" + mCenterLatLngs.size());
        }

    }

    /**
     * 绘制飞行航线
     */
    private void drawFlyWayLine() {
        if (wayPointLineList.size() > 0) {
            LogUtil.e("航点", "==============>" + wayPointLineList.size());
            if (mStartMarker != null) {
                mStartMarker.setPosition(wayPointLineList.get(0));
            } else {
                mStartMarker = MapCreateManager.getMapManager().setStartAndEndMarkerView(App.getContext(), wayPointLineList.get(0), "S");
            }
        }
        if (wayPointLineList.size() > 1) {
            if (mEndMarker != null) {
                mEndMarker.setPosition(wayPointLineList.get(wayPointLineList.size() - 1));
            } else {
                mEndMarker = MapCreateManager.getMapManager().setStartAndEndMarkerView(App.getContext(), wayPointLineList.get(wayPointLineList.size() - 1), "E");
            }
        }
        if (mWayLinePolyline == null) {
            //画航线
            mWayLinePolyline = MapCreateManager.getMapManager().drawTwoWayPointBlueLine(wayPointLineList);
        } else {
            mWayLinePolyline.setPoints(wayPointLineList);
        }
        //变化通知
        if (mStripLineChangeListener != null) {
            mStripLineChangeListener.OnStripLineChange();
        }

    }

    /**
     * 画中线和中点
     *
     * @param latLng
     */
    private void drawCenterPointLine(LatLng latLng) {
        mCenterLatLngs.add(latLng);
        Marker marker = addMarker(App.getContext(), latLng, mMarkerList.size() + 1, true);
        mMarkerList.add(marker);
        drawPlusAndLine();
    }

    private void drawPlusAndLine() {
        LogUtil.e("航点", "==============>" + mCenterLatLngs.size());
        if (mCenterLatLngs.size() > 1) {
            LatLng latLng1 = mCenterLatLngs.get(mCenterLatLngs.size() - 2);
            LatLng latLng2 = mCenterLatLngs.get(mCenterLatLngs.size() - 1);
            double distance = MapCalculationLogLatDistance.GetDistanceTwo(latLng1.longitude, latLng1.latitude, latLng2.longitude, latLng2.latitude);
            Polyline polyline = MapCreateManager.getMapManager().drawTwoPointBlue10Line(mCenterLatLngs);
            checkLineLength(distance, polyline);
            if (polyline != null) {
                mCenterPolyline.add(polyline);
            }
            Marker plusSignMarker = addMiddleMarker(App.getContext(), latLng1, latLng2, mCenterLatLngs.size() - 1, distance);
            if (plusSignMarker != null) {
                mMiddleWayPointsLatLngs.add(plusSignMarker.getPosition());
                markerMiddleWayPoints.add(plusSignMarker);
            }
        }
    }


    /**
     * 增加区域点
     *
     * @param context
     * @param latLng
     * @param position
     * @return
     */
    public Marker addMarker(Context context, LatLng latLng, int position, boolean isDrag) {
        return getPointMarkerView(context, latLng, position, isDrag);
    }

    public Marker getPointMarkerView(Context context, LatLng latLng, int position, boolean isDrag) {
        DroneRegionPointMarkerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_region_point_marker, null, false);
        binding.tvName.setText(position + "");
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(binding.getRoot())).position(latLng)
                .draggable(isDrag).anchor(0.5f, 0.5f);
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setZIndex(8);
        marker.setPeriod(position);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT);
        return marker;
    }

    private void getLeftEdgFlightBeltLine(LatLng latLng11, LatLng latLng22, double angle) {
        //距离
        LatLng latLonPointleft1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng11.longitude, latLng11.latitude, leftExpansion, angle + 90);
        LatLng latLonPointleft2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng22.longitude, latLng22.latitude, leftExpansion, angle + 90);

        mLeftLatLngs.add(latLonPointleft1);
        mLeftLatLngs.add(latLonPointleft2);
        LogUtil.e("航点", "==============>" + mCenterLatLngs.size());
    }

    private void getRightEdgFlightBeltLine(LatLng latLng11, LatLng latLng22, double angle) {
        //距离
        LatLng latLonPointright1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng11.longitude, latLng11.latitude, rightExpansion, angle - 90);
        LatLng latLonPointright2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng22.longitude, latLng22.latitude, rightExpansion, angle - 90);
        mRightLatLngs.add(latLonPointright1);
        mRightLatLngs.add(latLonPointright2);
    }

    /**
     * 右
     */
    private void calculationRightWayPolyLine() {
        if (rightWayLinesPolyline != null) {
            for (int i = 0; i < rightWayLinesPolyline.size(); i++) {
                rightWayLinesPolyline.get(i).remove();
            }
            rightWayLinesPolyline.clear();
        } else {
            rightWayLinesPolyline = new ArrayList<>();
        }
        if (rightWayLinesBeans.size() > 0) {
            if (leftWayLinesBeans.size() % 2 == 0) {
                for (int i = 0; i < rightWayLinesBeans.size(); i++) {
                    List<LatLng> latLngs = rightWayLinesBeans.get(i).getLatLngList();
                    if (i % 2 == 0) {
                        List<LatLng> tempLatLng = new ArrayList<>(latLngs);
                        Collections.reverse(tempLatLng);
                        wayPointLineList.addAll(tempLatLng);
                    } else {
                        wayPointLineList.addAll(latLngs);
                    }
                }
            } else {
                for (int i = 0; i < rightWayLinesBeans.size(); i++) {
                    List<LatLng> latLngs = rightWayLinesBeans.get(i).getLatLngList();
                    if (i % 2 == 0) {
                        wayPointLineList.addAll(latLngs);
                    } else {
                        List<LatLng> tempLatLng = new ArrayList<>(latLngs);
                        Collections.reverse(tempLatLng);
                        wayPointLineList.addAll(tempLatLng);
                    }
                }
            }

        }
    }

    /**
     * 右
     */
    private void calculationRightWayPointPolyLine(LatLng latLng0, LatLng latLng1, LatLng latLng2) {
        //计算线交点
        for (int i = 0; i < rightWayLinesBeans.size(); i++) {
            LatLngLineBean latLngLineBean = rightWayLinesBeans.get(i);
            double expansionLength = latLngLineBean.getExpandLength();
            List<LatLng> lineLatLngs = latLngLineBean.getLatLngList();
            //内角
            int innerAngle = (int) MapCalculationLogLatDistance.getTriangleAngle(latLng0.latitude, latLng0.longitude, latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
            double angleDifference = mAngles.get(mAngles.size() - 2) - mAngles.get(mAngles.size() - 1);
            boolean b = (angleDifference > 0 && Math.abs(angleDifference) <= 180) || (angleDifference < 0 && Math.abs(angleDifference) > 180);
            if (innerAngle >= 178 || innerAngle <= 2) {
                lineLatLngs.remove(lineLatLngs.size() - 2);
            } else {
                if (innerAngle < 70) {
                    if (b) {//右拐
                        LatLng intersectionLatLng = getLineIntersection(lineLatLngs);
                        if (intersectionLatLng == null) {
                            LatLng latLngPoint1 = mLeftLatLngs.get(mLeftLatLngs.size() - 1);
                            LatLng latLngPoint2 = mRightLatLngs.get(mRightLatLngs.size() - 1);
                            LatLng latLngPoint3 = lineLatLngs.get(lineLatLngs.size() - 4);
                            LatLng latLngPoint4 = lineLatLngs.get(lineLatLngs.size() - 3);
                            intersectionLatLng = getLineIntersection(latLngPoint1, latLngPoint2, latLngPoint3, latLngPoint4);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            if (intersectionLatLng != null && MapCreateManager.getMapManager().checkGpsCoordinate(intersectionLatLng.latitude, intersectionLatLng.longitude)) {
                                LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                                if (checkLineLength(latLngNet,intersectionLatLng)){
                                    lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                                }

                            }
                        } else {
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                            if (checkLineLength(latLngNet,intersectionLatLng)){
                                lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                            }

                        }
                    } else {//左拐
                        LatLng extendLatLng1 = lineLatLngs.get(lineLatLngs.size() - 3);
                        LatLng rightExtension1 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng1.longitude, extendLatLng1.latitude, expansionLength, mAngles.get(mAngles.size() - 2));
                        LatLng extendLatLng2 = lineLatLngs.get(lineLatLngs.size() - 2);
                        LatLng rightExtension2 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng2.longitude, extendLatLng2.latitude, expansionLength, mAngles.get(mAngles.size() - 1) + 180);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        lineLatLngs.add(lineLatLngs.size() - 1, rightExtension1);
                        lineLatLngs.add(lineLatLngs.size() - 1, rightExtension2);
                    }
                } else {
                    if (b) {  //右拐
                        LatLng intersectionLatLng = getLineIntersection(lineLatLngs);
                        if (intersectionLatLng == null) {
                            LatLng latLngPoint1 = mLeftLatLngs.get(mLeftLatLngs.size() - 1);
                            LatLng latLngPoint2 = mRightLatLngs.get(mRightLatLngs.size() - 1);
                            LatLng latLngPoint3 = lineLatLngs.get(lineLatLngs.size() - 4);
                            LatLng latLngPoint4 = lineLatLngs.get(lineLatLngs.size() - 3);
                            intersectionLatLng = getLineIntersection(latLngPoint1, latLngPoint2, latLngPoint3, latLngPoint4);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            if (intersectionLatLng != null && MapCreateManager.getMapManager().checkGpsCoordinate(intersectionLatLng.latitude, intersectionLatLng.longitude)) {
                                LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                                if (checkLineLength(latLngNet,intersectionLatLng)){
                                    lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                                }

                            }
                        } else {
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                            if (checkLineLength(latLngNet,intersectionLatLng)){
                                lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                            }

                        }
                    } else {//左拐
                        LatLng intersectionLatLng = getIntersectionLatLng(lineLatLngs);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                        if (checkLineLength(latLngNet,intersectionLatLng)){
                            lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                        }

                    }
                }
            }
        }

    }

    /**
     * 得到右
     * 航线点
     */
    private void getRightLinePoint(LatLng latLng11, LatLng latLng22, double angle, int size) {
        int lines = 0;
        if (routeIntervalDistance == 0 || routeIntervalDistance > rightExpansion) {
            lines = 0;
        } else {
            lines = (int) (rightExpansion / routeIntervalDistance);
        }
        if (lines > 0) {
            for (int i = 0; i < lines; i++) {
                double expandDiatance = routeIntervalDistance + routeIntervalDistance * i;
                LatLng latLonPointright1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng11.longitude, latLng11.latitude, expandDiatance, angle - 90);
                LatLng latLonPointright2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng22.longitude, latLng22.latitude, expandDiatance, angle - 90);
                if (size < 3) {
                    LatLngLineBean latLngLineBean = new LatLngLineBean();
                    List<LatLng> latLngs = new ArrayList<>();
                    latLngs.add(latLonPointright1);
                    latLngs.add(latLonPointright2);
                    latLngLineBean.setExpandLength(expandDiatance);
                    latLngLineBean.setLatLngList(latLngs);
                    rightWayLinesBeans.add(latLngLineBean);
                } else {
                    LatLngLineBean latLngLineBean = rightWayLinesBeans.get(i);
                    latLngLineBean.getLatLngList().add(latLonPointright1);
                    latLngLineBean.getLatLngList().add(latLonPointright2);
                }
            }
        }
    }


    /**
     * 得到左航线点
     *
     * @param latLng11
     * @param latLng22
     * @param angle
     * @param size
     */
    private void getLeftLinePoint(LatLng latLng11, LatLng latLng22, double angle, int size) {
        int lines = 0;
        if (routeIntervalDistance == 0 || routeIntervalDistance > leftExpansion) {
            lines = 0;
        } else {
            lines = (int) (leftExpansion / routeIntervalDistance);
        }
        if (lines > 0) {
            for (int i = 0; i < lines; i++) {
                double expandDiatance = routeIntervalDistance + routeIntervalDistance * i;
                LatLng latLonPointLeft1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng11.longitude, latLng11.latitude, expandDiatance, angle + 90);
                LatLng latLonPointLeft2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng22.longitude, latLng22.latitude, expandDiatance, angle + 90);
                if (size < 3) {// 0 1 2 3 4 5
                    LatLngLineBean latLngLineBean = new LatLngLineBean();
                    List<LatLng> latLngs = new ArrayList<>();
                    latLngs.add(latLonPointLeft1);
                    latLngs.add(latLonPointLeft2);
                    latLngLineBean.setExpandLength(expandDiatance);
                    latLngLineBean.setLatLngList(latLngs);
                    leftWayLinesBeans.add(latLngLineBean);
                } else {
                    LatLngLineBean latLngLineBean = leftWayLinesBeans.get(i);
                    latLngLineBean.getLatLngList().add(latLonPointLeft1);
                    latLngLineBean.getLatLngList().add(latLonPointLeft2);
                }
            }
        }
    }

    /**
     * 计算左航线
     */
    private void calculationLeftWayPointPolyLine(LatLng latLng0, LatLng latLng1, LatLng latLng2) {
        //计算线交点 角度计算
        for (int i = 0; i < leftWayLinesBeans.size(); i++) {
            LatLngLineBean latLngLineBean = leftWayLinesBeans.get(i);
            List<LatLng> lineLatLngs = latLngLineBean.getLatLngList();
            double expansionLength = latLngLineBean.getExpandLength();
            int innerAngle = (int) MapCalculationLogLatDistance.getTriangleAngle(latLng0.latitude, latLng0.longitude, latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
            double angleDifference = mAngles.get(mAngles.size() - 2) - mAngles.get(mAngles.size() - 1);
            boolean b = (angleDifference > 0 && Math.abs(angleDifference) <= 180) || (angleDifference < 0 && Math.abs(angleDifference) > 180);
            if (innerAngle >= 178 || innerAngle <= 2) {
                lineLatLngs.remove(lineLatLngs.size() - 2);
            } else {
                if (innerAngle < 70) {//右拐
                    if (b) {
                        LatLng extendLatLng1 = lineLatLngs.get(lineLatLngs.size() - 3);
                        LatLng leftExtension1 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng1.longitude, extendLatLng1.latitude, expansionLength, mAngles.get(mAngles.size() - 2));
                        LatLng extendLatLng2 = lineLatLngs.get(lineLatLngs.size() - 2);
                        LatLng leftExtension2 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng2.longitude, extendLatLng2.latitude, expansionLength, mAngles.get(mAngles.size() - 1) + 180);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        lineLatLngs.add(lineLatLngs.size() - 1, leftExtension1);
                        lineLatLngs.add(lineLatLngs.size() - 1, leftExtension2);
                    } else {//左拐
                        LatLng intersectionLatLng = getLineIntersection(lineLatLngs);
                        if (intersectionLatLng == null) {
                            LatLng latLngPoint1 = mLeftLatLngs.get(mLeftLatLngs.size() - 1);
                            LatLng latLngPoint2 = mRightLatLngs.get(mRightLatLngs.size() - 1);
                            LatLng latLngPoint3 = lineLatLngs.get(lineLatLngs.size() - 4);
                            LatLng latLngPoint4 = lineLatLngs.get(lineLatLngs.size() - 3);
                            intersectionLatLng = getLineIntersection(latLngPoint1, latLngPoint2, latLngPoint3, latLngPoint4);

                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            if (intersectionLatLng != null && MapCreateManager.getMapManager().checkGpsCoordinate(intersectionLatLng.latitude, intersectionLatLng.longitude)) {
                                LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                                if (checkLineLength(latLngNet,intersectionLatLng)){
                                    lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                                }

                            }
                        } else {
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                            if (checkLineLength(latLngNet,intersectionLatLng)){
                                lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                            }

                        }
                    }
                } else {
                    if (b) {//右拐
                        LatLng intersectionLatLng = getIntersectionLatLng(lineLatLngs);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        lineLatLngs.remove(lineLatLngs.size() - 2);
                        LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                        if (checkLineLength(latLngNet,intersectionLatLng)){
                            lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                        }

                    } else {//左拐
                        LatLng intersectionLatLng = getLineIntersection(lineLatLngs);
                        if (intersectionLatLng == null) {
                            LatLng latLngPoint1 = mLeftLatLngs.get(mLeftLatLngs.size() - 1);
                            LatLng latLngPoint2 = mRightLatLngs.get(mRightLatLngs.size() - 1);
                            LatLng latLngPoint3 = lineLatLngs.get(lineLatLngs.size() - 4);
                            LatLng latLngPoint4 = lineLatLngs.get(lineLatLngs.size() - 3);
                            intersectionLatLng = getLineIntersection(latLngPoint1, latLngPoint2, latLngPoint3, latLngPoint4);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            if (intersectionLatLng != null && MapCreateManager.getMapManager().checkGpsCoordinate(intersectionLatLng.latitude, intersectionLatLng.longitude)) {
                                LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                                if (checkLineLength(latLngNet,intersectionLatLng)){
                                    lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                                }

                            }
                        } else {
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            lineLatLngs.remove(lineLatLngs.size() - 2);
                            LatLng latLngNet = lineLatLngs.get(lineLatLngs.size() - 1);
                            if (checkLineLength(latLngNet,intersectionLatLng)){
                                lineLatLngs.add(lineLatLngs.size() - 1, intersectionLatLng);
                            }

                        }
                    }
                }
            }
        }
    }


    /**
     * 计算左航线
     */
    private void calculationLeftWayPolyLine() {
        if (leftWayLinesPolyline != null) {
            for (int i = 0; i < leftWayLinesPolyline.size(); i++) {
                leftWayLinesPolyline.get(i).remove();
            }
            leftWayLinesPolyline.clear();
        } else {
            leftWayLinesPolyline = new ArrayList<>();
        }
        for (int i = 0; i < leftWayLinesMarkers.size(); i++) {
            leftWayLinesMarkers.get(i).remove();
        }
        leftWayLinesMarkers.clear();
        if (leftWayLinesBeans.size() > 0) {
            if (leftWayLinesBeans.size() % 2 == 0) {//偶数条
                for (int i = leftWayLinesBeans.size() - 1; i >= 0; i--) {
                    List<LatLng> latLngs = leftWayLinesBeans.get(i).getLatLngList();
                    if (i % 2 == 0) {//偶数
                        List<LatLng> tempLatLng = new ArrayList<>(latLngs);
                        Collections.reverse(tempLatLng);
                        wayPointLineList.addAll(tempLatLng);
                    } else {
                        wayPointLineList.addAll(latLngs);
                    }
                }
            } else {
                for (int i = leftWayLinesBeans.size() - 1; i >= 0; i--) {
                    List<LatLng> latLngs = leftWayLinesBeans.get(i).getLatLngList();
                    if (i % 2 == 0) {//偶数
                        wayPointLineList.addAll(latLngs);
                    } else {
                        List<LatLng> tempLatLng = new ArrayList<>(latLngs);
                        Collections.reverse(tempLatLng);
                        wayPointLineList.addAll(tempLatLng);
                    }
                }
            }

        }
    }


    /**
     * 计算边线点
     *
     * @param innerAngle
     * @param isRightTurn
     */
    private void calculationLeftEdgPointPolyLine(int innerAngle, boolean isRightTurn) {
        LogUtil.e("边线", "左=======================================" + innerAngle);
        if (innerAngle >= 178 || innerAngle <= 2) {
            mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
        } else {
            //63度
            if (innerAngle < 70) { //两个拐点
                if (isRightTurn) {//右拐
                    LogUtil.e("边线", "左======================右拐=================");
                    //交点
                    LatLng extendLatLng1 = mLeftLatLngs.get(mLeftLatLngs.size() - 3);
                    LatLng leftExtension1 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng1.longitude, extendLatLng1.latitude, leftExpansion, mAngles.get(mAngles.size() - 2));
                    LatLng extendLatLng2 = mLeftLatLngs.get(mLeftLatLngs.size() - 2);
                    LatLng leftExtension2 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng2.longitude, extendLatLng2.latitude, leftExpansion, mAngles.get(mAngles.size() - 1) + 180);
                    mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                    mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                    mLeftLatLngs.add(mLeftLatLngs.size() - 1, leftExtension1);
                    mLeftLatLngs.add(mLeftLatLngs.size() - 1, leftExtension2);
                } else {////左拐
                    LogUtil.e("边线", "左======================左拐=================");
                    LatLng leftTurnLatLng = getLineIntersection(mLeftLatLngs);
                    if (leftTurnLatLng == null) {
                        LogUtil.e("边线", "左======================左拐=====1============");

                        LatLng latLngPoint1 = mLeftLatLngs.get(mLeftLatLngs.size() - 2);
                        LatLng latLngPoint2 = mLeftLatLngs.get(mLeftLatLngs.size() - 1);

                        leftTurnLatLng = MapCalculationLogLatDistance.getDistanceExpansionPoint(latLngPoint1.longitude, latLngPoint1.latitude, latLngPoint2.longitude, latLngPoint2.latitude, rightExpansion);

                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        if (leftTurnLatLng != null) {
                            mLeftLatLngs.add(leftTurnLatLng);
                            LogUtil.e("边线", "左======================左拐=======4==========");
                        } else {

                            LogUtil.e("边线", "左======================左拐=======5==========");
                        }
                    } else {
                        LogUtil.e("边线", "左======================左拐=====2============");
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.add(mLeftLatLngs.size() - 1, leftTurnLatLng);
                    }
                }
            } else { //一个拐点 右拐
                if (isRightTurn) {
                    LogUtil.e("边线", "左======================右拐=================" + innerAngle);
                    LatLng leftTurnLatLng = getIntersectionLatLng(mLeftLatLngs);
                    if (leftTurnLatLng != null && MapCreateManager.getMapManager().checkGpsCoordinate(leftTurnLatLng.latitude, leftTurnLatLng.longitude)) {
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.add(mLeftLatLngs.size() - 1, leftTurnLatLng);
                    }

                } else {//左拐
                    LogUtil.e("边线", "左======================左拐=================" + innerAngle);
                    LatLng leftTurnLatLng = getLineIntersection(mLeftLatLngs);
                    if (leftTurnLatLng == null) {
                        LogUtil.e("边线", "左======================左拐=======1==========");
                        LatLng latLngPoint1 = mLeftLatLngs.get(mLeftLatLngs.size() - 2);
                        LatLng latLngPoint2 = mLeftLatLngs.get(mLeftLatLngs.size() - 1);

                        leftTurnLatLng = MapCalculationLogLatDistance.getDistanceExpansionPoint(latLngPoint1.longitude, latLngPoint1.latitude, latLngPoint2.longitude, latLngPoint2.latitude, rightExpansion);

                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                        if (leftTurnLatLng != null) {
                            LogUtil.e("边线", "左======================左拐=======3==========");
                            mLeftLatLngs.add(leftTurnLatLng);
                        }
                    } else {
                        LogUtil.e("边线", "左======================左拐=======2==========");
                        if (MapCreateManager.getMapManager().checkGpsCoordinate(leftTurnLatLng.latitude, leftTurnLatLng.longitude)) {
                            mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                            mLeftLatLngs.remove(mLeftLatLngs.size() - 2);
                            mLeftLatLngs.add(mLeftLatLngs.size() - 1, leftTurnLatLng);
                        }


                    }
                }
            }
        }

    }

    /**
     * 计算右边线点
     *
     * @param innerAngle  内角
     * @param isRightTurn 是否右拐
     */
    private void calculationRightEdgPointPolyLine(int innerAngle, boolean isRightTurn) {
        LogUtil.e("边线", "右=======================================" + innerAngle);
        if (innerAngle >= 178 || innerAngle <= 2) {
            mRightLatLngs.remove(mRightLatLngs.size() - 2);
        } else {
            //63度
            if (innerAngle < 70) {
                if (isRightTurn) {//右拐
                    LogUtil.e("边线", "右======================右拐=================");
                    LatLng rightTurnLatLng = getLineIntersection(mRightLatLngs);
                    if (rightTurnLatLng == null) {
                        LogUtil.e("边线", "右======================右拐=================");
                        LatLng latLngPoint1 = mRightLatLngs.get(mRightLatLngs.size() - 2);
                        LatLng latLngPoint2 = mRightLatLngs.get(mRightLatLngs.size() - 1);

                        rightTurnLatLng = MapCalculationLogLatDistance.getDistanceExpansionPoint(latLngPoint1.longitude, latLngPoint1.latitude, latLngPoint2.longitude, latLngPoint2.latitude, rightExpansion);

                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.remove(mRightLatLngs.size() - 1);
                        if (rightTurnLatLng != null) {
                            LogUtil.e("边线", "右======================右拐=================");
                            mRightLatLngs.add(mRightLatLngs.size(), rightTurnLatLng);
                        } else {

                            LogUtil.e("边线", "右======================右拐=================");
                        }
                    } else {
                        LogUtil.e("边线", "右======================右拐=================");
                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.add(mRightLatLngs.size() - 1, rightTurnLatLng);
                    }
                } else {//左拐
                    LogUtil.e("边线", "右======================左拐=================");
                    LatLng extendLatLng1 = mRightLatLngs.get(mRightLatLngs.size() - 3);
                    LatLng rightExtension1 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng1.longitude, extendLatLng1.latitude, rightExpansion, mAngles.get(mAngles.size() - 2));
                    LatLng extendLatLng2 = mRightLatLngs.get(mRightLatLngs.size() - 2);
                    LatLng rightExtension2 = MapCalculationLogLatDistance.convertDistanceToLogLat(extendLatLng2.longitude, extendLatLng2.latitude, rightExpansion, mAngles.get(mAngles.size() - 1) + 180);
                    mRightLatLngs.remove(mRightLatLngs.size() - 2);
                    mRightLatLngs.remove(mRightLatLngs.size() - 2);
                    mRightLatLngs.add(mRightLatLngs.size() - 1, rightExtension1);
                    mRightLatLngs.add(mRightLatLngs.size() - 1, rightExtension2);
                }
            } else {
                if (isRightTurn) {//右拐
                    LogUtil.e("边线", "右======================右拐=================");
                    LatLng rightTurnLatLng = getLineIntersection(mRightLatLngs);
                    if (rightTurnLatLng == null) {
                        LogUtil.e("边线", "右======================右拐=================");

                        LatLng latLngPoint1 = mRightLatLngs.get(mRightLatLngs.size() - 2);
                        LatLng latLngPoint2 = mRightLatLngs.get(mRightLatLngs.size() - 1);

                        rightTurnLatLng = MapCalculationLogLatDistance.getDistanceExpansionPoint(latLngPoint1.longitude, latLngPoint1.latitude, latLngPoint2.longitude, latLngPoint2.latitude, rightExpansion);

                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.remove(mRightLatLngs.size() - 1);
                        if (rightTurnLatLng != null) {
                            LogUtil.e("边线", "右======================右拐=================");
                            mRightLatLngs.add(mRightLatLngs.size() - 1, rightTurnLatLng);
                        } else {
                            LogUtil.e("边线", "右======================右拐=================");
                        }
                    } else {
                        LogUtil.e("边线", "右======================右拐=================");
                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.remove(mRightLatLngs.size() - 2);
                        mRightLatLngs.add(mRightLatLngs.size() - 1, rightTurnLatLng);
                    }
                } else {   //左拐
                    LogUtil.e("边线", "右======================左拐=================");
                    LatLng rightTurnLatLng = getIntersectionLatLng(mRightLatLngs);
                    mRightLatLngs.remove(mRightLatLngs.size() - 2);
                    mRightLatLngs.remove(mRightLatLngs.size() - 2);
                    mRightLatLngs.add(mRightLatLngs.size() - 1, rightTurnLatLng);
                }
            }
        }
    }

    /**
     * 画边线到地图上
     */
    private void drawLeftEdgStripLineToMap() {
        LogUtil.e("航点", "==============>" + mLeftLatLngs.size());
        if (mLeftPolyline != null) {
            mLeftPolyline.setPoints(mLeftLatLngs);
        } else {
            mLeftPolyline = MapCreateManager.getMapManager().drawDashBlueLine(mLeftLatLngs);
        }

    }


    private void drawRightEdgStripLineToMap() {
        if (mRightPolyline != null) {
            mRightPolyline.setPoints(mRightLatLngs);
        } else {
            mRightPolyline = MapCreateManager.getMapManager().drawDashBlueLine(mRightLatLngs);
        }
    }

    /**
     * 画边缘封闭线
     */
    public void drawEdgClosedLineToMap() {
        if (mLeftLatLngs != null && mLeftLatLngs.size() > 0 && mRightLatLngs != null && mRightLatLngs.size() > 0) {
            //区域和边缘线
            List<LatLng> startPoints = new ArrayList<LatLng>();
            startPoints.add(mLeftLatLngs.get(0));
            startPoints.add(mRightLatLngs.get(0));
            if (mStartPolyline != null) {
                mStartPolyline.setPoints(startPoints);
            } else {
                mStartPolyline = MapCreateManager.getMapManager().drawDashBlueLine(startPoints);
            }
            if (mLeftLatLngs.size() > 1 && mRightLatLngs.size() > 1) {
                List<LatLng> endPoints = new ArrayList<LatLng>();
                endPoints.add(mLeftLatLngs.get(mLeftLatLngs.size() - 1));
                endPoints.add(mRightLatLngs.get(mRightLatLngs.size() - 1));
                if (mEndPolyline != null) {
                    mEndPolyline.setPoints(endPoints);
                } else {
                    mEndPolyline = MapCreateManager.getMapManager().drawDashBlueLine(endPoints);
                }
            }
        }

    }

    /**
     * 直线交点
     *
     * @param latLngs
     * @return
     */
    private LatLng getIntersectionLatLng(List<LatLng> latLngs) {
        LatLng latLng = null;
        if (latLngs.size() > 3) {
            LatLng leftLatLng1 = latLngs.get(latLngs.size() - 4);
            LatLng leftLatLng2 = latLngs.get(latLngs.size() - 3);
            LatLng leftLatLng3 = latLngs.get(latLngs.size() - 2);
            LatLng leftLatLng4 = latLngs.get(latLngs.size() - 1);
            double[] double1s = MapCalculationLogLatDistance.intersection(leftLatLng1.longitude, leftLatLng1.latitude, leftLatLng2.longitude, leftLatLng2.latitude,
                    leftLatLng3.longitude, leftLatLng3.latitude, leftLatLng4.longitude, leftLatLng4.latitude);
            //左拐点
            if (double1s != null) {
                latLng = new LatLng(double1s[1], double1s[0]);
//                Marker edgMarker1 = ObliquePhotographyUtil.getInstance().addMarker(App.getContext(), leftTurnLatLng, 1, false);
            }
        }
        return latLng;
    }

    /**
     * 线段交点 不包括端点
     *
     * @param latLngs
     * @return
     */
    private LatLng getLineIntersection(List<LatLng> latLngs) {
        LatLng latLng = null;
        if (latLngs.size() > 3) {
            LatLng leftLatLng1 = latLngs.get(latLngs.size() - 4);
            LatLng leftLatLng2 = latLngs.get(latLngs.size() - 3);
            LatLng leftLatLng3 = latLngs.get(latLngs.size() - 2);
            LatLng leftLatLng4 = latLngs.get(latLngs.size() - 1);
            double[] double1s = MapCalculationLogLatDistance.getLineIntersection(leftLatLng1.longitude, leftLatLng1.latitude, leftLatLng2.longitude, leftLatLng2.latitude,
                    leftLatLng3.longitude, leftLatLng3.latitude, leftLatLng4.longitude, leftLatLng4.latitude);
            //左拐点
            if (double1s != null) {
                latLng = new LatLng(double1s[1], double1s[0]);
            }
        }

        return latLng;
    }

    /**
     * 得到交点 不包括端点
     *
     * @return
     */
    private LatLng getLineIntersection(LatLng leftLatLng1, LatLng leftLatLng2, LatLng leftLatLng3, LatLng leftLatLng4) {
        LatLng latLng = null;
        double[] double1s = MapCalculationLogLatDistance.getLineIntersection(leftLatLng1.longitude, leftLatLng1.latitude, leftLatLng2.longitude, leftLatLng2.latitude,
                leftLatLng3.longitude, leftLatLng3.latitude, leftLatLng4.longitude, leftLatLng4.latitude);
        //左拐点
        if (double1s != null) {
            latLng = new LatLng(double1s[1], double1s[0]);
        }
        return latLng;
    }


    /**
     * 撤销
     */
    public void revokeCenterLatLng() {
        if (mCenterLatLngs != null && mCenterLatLngs.size() > 0) {
            if (revokePosition >= 0 && revokePosition < mCenterLatLngs.size()) {
                mCenterLatLngs.remove(revokePosition);
                revokePosition = mCenterLatLngs.size() - 1;
            } else {
                revokePosition = -1;
            }
        } else {
            revokePosition = -1;
        }
        if (mCenterLatLngs != null && mCenterLatLngs.size() > 0) {
            reClickAddMarkerDrawPoint();
        } else {
            clearMap();
        }
    }

    /**
     * Marker+点击
     *
     * @param marker
     */

    public void onMarkerClick(Marker marker) {
        int clickPosition = marker.getPeriod();
        LogUtil.e("数据", "==============" + clickPosition);
        LatLng latLng = marker.getPosition();
        mCenterLatLngs.add(clickPosition, latLng);
        revokePosition = clickPosition - 1;
        reClickAddMarkerDrawPoint();
    }

    /**
     * 开始拖动
     *
     * @param marker
     */
    public void onMarkerDragStart(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT) {
                    int clickPosition = marker.getPeriod();
                    mStartLatLng = mCenterLatLngs.get(clickPosition - 1);
                    marker.setPosition(mStartLatLng);

                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT_ADD) {
                    int clickPosition = marker.getPeriod();
                    mStartLatLng = mMiddleWayPointsLatLngs.get(clickPosition - 1);
                    marker.setPosition(mStartLatLng);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 拖动中
     *
     * @param marker
     */
    public void onMarkerDrag(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT) {
                    int clickPosition = marker.getPeriod();
                    LogUtil.e("拖动", "==================>" + clickPosition);
                    LatLng dragLatLng = marker.getPosition();
                    mCenterLatLngs.set(clickPosition - 1, dragLatLng);

                    int totalSize = mCenterLatLngs.size();
                    if (clickPosition == 1) {

                        LatLng latLng = mCenterLatLngs.get(clickPosition);

                        LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, latLng);
                        Marker middleMarker = markerMiddleWayPoints.get(clickPosition - 1);
                        middleMarker.setPosition(middleLatLng);
                        mMiddleWayPointsLatLngs.set(clickPosition - 1, middleLatLng);
                        double distance = MapCalculationLogLatDistance.GetDistanceTwo(latLng.longitude, latLng.latitude, dragLatLng.longitude, dragLatLng.latitude);
                        middleMarker.setIcon(getMiddlePlusSignMarkerView(App.getContext(), distance));
                        //连线
                        List<LatLng> latLngs1 = new ArrayList<>();
                        latLngs1.add(latLng);
                        latLngs1.add(dragLatLng);
                        Polyline polyline1 = mCenterPolyline.get(clickPosition - 1);
                        polyline1.setPoints(latLngs1);
                        checkLineLength(distance, polyline1);
                    } else {
                        if (clickPosition == totalSize) {
                            LatLng latLng = mCenterLatLngs.get(clickPosition - 2);


                            LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, latLng);
                            Marker middleMarker = markerMiddleWayPoints.get(clickPosition - 2);
                            middleMarker.setPosition(middleLatLng);
                            mMiddleWayPointsLatLngs.set(clickPosition - 2, middleLatLng);
                            double distance = MapCalculationLogLatDistance.GetDistanceTwo(latLng.longitude, latLng.latitude, dragLatLng.longitude, dragLatLng.latitude);
                            middleMarker.setIcon(getMiddlePlusSignMarkerView(App.getContext(), distance));
                            //连线
                            List<LatLng> latLngs1 = new ArrayList<>();
                            latLngs1.add(latLng);
                            latLngs1.add(dragLatLng);
                            Polyline polyline1 = mCenterPolyline.get(clickPosition - 2);
                            polyline1.setPoints(latLngs1);
                            checkLineLength(distance, polyline1);

                        } else {
                            int prevPosition = clickPosition - 2;
                            //再次计算带加号的中点
                            LatLng latLng1 = mCenterLatLngs.get(prevPosition);
                            LatLng latLng2 = mCenterLatLngs.get(clickPosition);
                            //计算第一个中点的坐标
                            LatLng middleLatLng1 = MapCalculationLogLatDistance.getMiddleLatLng(latLng1, dragLatLng);
                            //计算第二个中点的坐标
                            LatLng middleLatLng2 = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, latLng2);
                            //getMiddlePlusSignMarkerView
                            int middlePosition2 = clickPosition - 1;
                            Marker middleMarker1 = markerMiddleWayPoints.get(prevPosition);
                            Marker middleMarker2 = markerMiddleWayPoints.get(middlePosition2);
                            mMiddleWayPointsLatLngs.set(clickPosition - 2, middleLatLng1);
                            mMiddleWayPointsLatLngs.set(clickPosition - 1, middleLatLng2);
                            middleMarker1.setPosition(middleLatLng1);
                            middleMarker2.setPosition(middleLatLng2);
                            double distance1 = MapCalculationLogLatDistance.GetDistanceTwo(latLng1.longitude, latLng1.latitude, dragLatLng.longitude, dragLatLng.latitude);
                            double distance2 = MapCalculationLogLatDistance.GetDistanceTwo(dragLatLng.longitude, dragLatLng.latitude, latLng2.longitude, latLng2.latitude);
                            middleMarker1.setIcon(getMiddlePlusSignMarkerView(App.getContext(), distance1));
                            middleMarker2.setIcon(getMiddlePlusSignMarkerView(App.getContext(), distance2));

                            List<LatLng> latLngs1 = new ArrayList<>();
                            latLngs1.add(latLng1);
                            latLngs1.add(dragLatLng);
                            Polyline polyline1 = mCenterPolyline.get(clickPosition - 2);
                            checkLineLength(distance1, polyline1);
                            polyline1.setPoints(latLngs1);
                            checkLineLength(distance1, polyline1);

                            List<LatLng> latLngs2 = new ArrayList<>();
                            latLngs2.add(dragLatLng);
                            latLngs2.add(latLng2);
                            Polyline polyline2 = mCenterPolyline.get(clickPosition - 1);
                            polyline2.setPoints(latLngs2);
                            checkLineLength(distance2, polyline2);
                        }
                    }
                }

                if (type == MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT_ADD) {
                    int clickPosition = marker.getPeriod();
                    //改变连线
                    LatLng gragLatLng = marker.getPosition();
                    LatLng latLng1 = mCenterLatLngs.get(clickPosition - 1);
                    LatLng latLng2 = mCenterLatLngs.get(clickPosition);
                    //第一条线
                    List<LatLng> latLngs = new ArrayList<>();
                    latLngs.add(latLng1);
                    latLngs.add(gragLatLng);
                    latLngs.add(latLng2);
                    Polyline polyline = mCenterPolyline.get(clickPosition - 1);
                    polyline.setPoints(latLngs);

                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 开始拖动
     *
     * @param marker
     */
    public void onMarkerDragEnd(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT) {
                    int clickPosition = marker.getPeriod();
                    LatLng dragLatLng = marker.getPosition();
                    mCenterLatLngs.set(clickPosition - 1, dragLatLng);
                    //计算距离
                    reDrawPoint();
                    //计算
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT_ADD) {
                    int clickPosition = marker.getPeriod();
                    LatLng latLng = marker.getPosition();
                    revokePosition = clickPosition - 1;
                    mCenterLatLngs.add(clickPosition, latLng);
                    reClickAddMarkerDrawPoint();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 长度
     *
     * @return
     */
    public double getLineLength() {
        return MapCreateManager.getMapManager().getLatLngsLengh(wayPointLineList);
    }

    public int getLineNumbers() {
        return wayPointLineList.size();
    }

    /**
     * 飞行时间
     *
     * @param distance
     * @param speed
     * @return 时分秒
     */
    public String getWayPointFlyTime(double distance, double speed) {
        String timeStr = "0";
        int time = 0;
        if (speed > 0) {
            time = (int) (distance / speed);
        }
        timeStr = DateUtils.getHMSByMillisecondTimeStamp(time * 1000);
        return timeStr;
    }

    /**
     * 获取面积
     *
     * @return
     */
    public double getStripRegionArea() {
        List<LatLng> latLngs = new ArrayList<>();
        if (mLeftLatLngs.size() > 0 && mRightLatLngs.size() > 0) {
            latLngs.addAll(mLeftLatLngs);
            List<LatLng> rightLatLngs = new ArrayList<>(mRightLatLngs);
            Collections.reverse(rightLatLngs);
            latLngs.addAll(rightLatLngs);
            latLngs.add(mLeftLatLngs.get(0));
        }
        return MapCreateManager.getMapManager().workOutArea(latLngs);
    }

    public int getWayPointPictures(double distance, double speed) {
        String timeStr = "0";
        int time = 0;
        if (speed > 0) {
            time = (int) (distance / speed);
        } else {
            speed = 6;
            time = (int) (distance / speed);
        }
        double courseIntervalTime = (courseIntervalDistance * 1.0) / speed;
        int pictures = (int) (time / courseIntervalTime);
        return pictures + getLineNumbers();
    }

    /**
     * 修改焦距
     *
     * @param focalLength
     */
    public void setFocalLength(double focalLength, double gsd) {
        this.focalLength = focalLength;
        this.baseGsd = gsd;
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
        setGsd(flyHeight);
    }

    /**
     * GSD
     *
     * @param flyHeight
     */
    private void setGsd(int flyHeight) {
        gsd = baseGsd * flyHeight * 0.01;
        DecimalFormat decimalFormat = new DecimalFormat("###0.00");
        gsd = Double.parseDouble(decimalFormat.format(gsd));
    }

    /**
     * GSD
     *
     * @return
     */
    public double getGsd() {
        return gsd;
    }

    /**
     * 修改高度
     *
     * @param flyHeight
     */
    public void setFlyHeight(int flyHeight) {
        this.flyHeight = flyHeight;
        setGsd(flyHeight);
        setRouteCourseIntervalDistance(flyHeight, focalLength, shortFormat, courseOverlapRatio);
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
        reDrawPoint();
    }

    /**
     * 改变高度和航向重叠率
     * 盘向间隔
     *
     * @param height           高度
     * @param focalLength      焦距
     * @param shortFormat      短画幅
     * @param diskOverlapRatio 重叠率
     */
    public void setRouteCourseIntervalDistance(double height, double focalLength, double shortFormat, double diskOverlapRatio) {
        double routeCourseTrueDistance = getRouteCourseTrueDistance(height, focalLength, shortFormat);
        courseIntervalDistance = (int) (routeCourseTrueDistance - routeCourseTrueDistance * diskOverlapRatio * 0.01);
    }

    /**
     * 航向拍摄真实距离
     *
     * @param height      高度
     * @param focalLength 焦距
     * @param shortFormat 短画幅
     * @param height
     */
    public double getRouteCourseTrueDistance(double height, double focalLength, double shortFormat) {
        focalLength = focalLength / 1000.0;
        shortFormat = shortFormat / 1000.0;
        return height * shortFormat / focalLength;
    }

    /**
     * 修改盘向重叠率
     *
     * @param diskOverlapRatio
     */
    public void setDiskOverlapRatio(int diskOverlapRatio) {
        this.diskOverlapRatio = diskOverlapRatio;
        setRouteIntervalDistance(flyHeight, focalLength, longFormat, diskOverlapRatio);
        reDrawPoint();
    }

    /**
     * 修改航向重叠率
     *
     * @param courseOverlapRatio
     */
    public void setCourseOverlapRatio(int courseOverlapRatio) {
        this.courseOverlapRatio = courseOverlapRatio;
        setRouteCourseIntervalDistance(flyHeight, focalLength, shortFormat, courseOverlapRatio);
        if (mStripLineChangeListener != null) {
            mStripLineChangeListener.OnStripLineChange();
        }
    }

    /**
     * 改变高度和盘向重叠率
     * 盘向间隔
     *
     * @param height           高度
     * @param focalLength      焦距
     * @param longFormat       长画幅
     * @param diskOverlapRatio 重叠率
     */
    public void setRouteIntervalDistance(double height, double focalLength, double longFormat, double diskOverlapRatio) {
        double routeTrueDistance = getRouteTrueDistance(height, focalLength, longFormat);
        routeIntervalDistance = (int) (routeTrueDistance - routeTrueDistance * diskOverlapRatio * 0.01);
    }

    /**
     * 盘向拍摄真实距离
     *
     * @param height      高度
     * @param focalLength 焦距
     * @param longFormat  长画幅
     * @param height
     */
    public double getRouteTrueDistance(double height, double focalLength, double longFormat) {
        focalLength = focalLength / 1000.0;
        longFormat = longFormat / 1000.0;
        return height * longFormat / focalLength;
    }


    private boolean checkLineLength(LatLng latLng1, LatLng latLng2) {
        double distance = MapCalculationLogLatDistance.GetDistanceTwo(latLng1.longitude, latLng1.latitude, latLng2.longitude, latLng2.latitude);
        if (distance > 2000) {
            return false;
        } else if (distance < 0.5) {
            return false;
        }
        return true;
    }

    /**
     * 检测航线长度
     * [0.5-2000]
     *
     * @param distance
     * @param polyline1
     */
    private void checkLineLength(double distance, Polyline polyline1) {
        if (distance > 2000) {
            polyline1.setColor(AndroidUtil.getColor(R.color.red));
            showTooLengthMsg();
        } else if (distance < 0.5) {
            polyline1.setColor(AndroidUtil.getColor(R.color.red));
            showTooShortMsg();
        } else {
            polyline1.setColor(AndroidUtil.getColor(R.color.deepskyblue));
        }
    }

    public void showTooShortMsg() {
//        TipsTaskView.getTask().setDuration(5000).setIsCreatView(false).setMsg("航点距离不能小于0.5m,请调整").doShow2();
    }

    public void showTooLengthMsg() {
//        TipsTaskView.getTask().setDuration(5000).setIsCreatView(false).setMsg("航点距离不能大于2000m,请调整").doShow2();
    }

    private Circle startCircle;
    private Circle endCircle;

    /**
     * @param centerpoint 中心点坐标
     * @param radius      半径 1公里(km)=1000米(m)
     */
    public void addPolylinesScircle(LatLng centerpoint, int radius) {
        if (startCircle == null) {
            startCircle = m3DAMap.addCircle(new CircleOptions()
                    .strokeColor(0x33000000)
                    .fillColor(0x33000000)
                    .strokeWidth(0));
        }
        startCircle.setRadius(radius - radius * 0.1);

        startCircle.setCenter(centerpoint);
    }

    /**
     * @param centerpoint 中心点坐标
     * @param radius      半径 1公里(km)=1000米(m)
     */
    public void addPolylinesEcircle(LatLng centerpoint, int radius) {
        if (endCircle == null) {
            endCircle = m3DAMap.addCircle(new CircleOptions()
                    .strokeColor(0x33000000)
                    .fillColor(0x33000000)
                    .strokeWidth(0));
        }
        endCircle.setRadius(radius - radius * 0.1);
        endCircle.setCenter(centerpoint);
    }


    public Polyline drawPolyLineOptions(AMap aMap, List<LatLng> linelatLngs,
                                        int w, int color) {
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.addAll(linelatLngs);
        polylineOptions.zIndex(7);
        polylineOptions.width(w).color(color);
        return aMap.addPolyline(polylineOptions);
    }

    /**
     * 添加中间的带加号的航点
     *
     * @param context
     * @param beforeLatLng
     * @param latterLatLng
     * @param position
     * @return
     */
    public Marker addMiddleMarker(Context context, LatLng beforeLatLng, LatLng latterLatLng, int position, double distance) {
        LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(beforeLatLng, latterLatLng);
        return getMiddlePlusSignMarkerView(context, middleLatLng, position, distance);
    }

    /**
     * 增加带加号航点的Marker按钮
     * 带加号的
     *
     * @param context
     * @param latLng
     * @param position
     * @param distance
     * @return
     */
    public Marker getMiddlePlusSignMarkerView(Context context, LatLng latLng, int position, double distance) {
        DroneMiddleMarkerBinding middlePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_middle_marker, null, false);
        DecimalFormat decimalFormat = new DecimalFormat("###0.0");
        middlePointMarkerBinding.tvMiddlePoint.setText(decimalFormat.format(distance) + "m");
        if (distance > 2000 || distance < 0.5) {
            middlePointMarkerBinding.tvMiddlePoint.setTextColor(AndroidUtil.getColor(R.color.red));
        } else {
            middlePointMarkerBinding.tvMiddlePoint.setTextColor(AndroidUtil.getColor(R.color.black));
        }
        MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromView(middlePointMarkerBinding.getRoot())).position(latLng)
                .draggable(true).anchor(0.5f, 0.7f);//第几个点 从1开始
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setSnippet(0 + "");
        marker.setClickable(true);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_FLIGHT_BELT_ADD);
        marker.setZIndex(5);
        marker.setPeriod(position);
        return marker;
    }

    public BitmapDescriptor getMiddlePlusSignMarkerView(Context context, double distance) {
        DroneMiddleMarkerBinding middlePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_middle_marker, null, false);
        DecimalFormat decimalFormat = new DecimalFormat("###0.0");
        middlePointMarkerBinding.tvMiddlePoint.setText(decimalFormat.format(distance) + "m");
        if (distance > 2000 || distance < 0.5) {
            middlePointMarkerBinding.tvMiddlePoint.setTextColor(AndroidUtil.getColor(R.color.red));
        } else {
            middlePointMarkerBinding.tvMiddlePoint.setTextColor(AndroidUtil.getColor(R.color.black));
        }
        return BitmapDescriptorFactory.fromView(middlePointMarkerBinding.getRoot());
    }

    public StripLineChangeListener mStripLineChangeListener;


    public void setStripLineChangeListener(StripLineChangeListener stripLineChangeListener) {
        mStripLineChangeListener = stripLineChangeListener;
    }

    public interface StripLineChangeListener {
        void OnStripLineChange();
    }

}
